package com.tengju.payment.infrastructure.repository.convert;


import com.tengju.payment.domain.model.BusinessType;
import com.tengju.payment.domain.model.ChannelType;
import com.tengju.payment.domain.model.HandleContext;
import com.tengju.payment.domain.model.TradeStatus;
import com.tengju.payment.domain.model.channel.ChannelId;
import com.tengju.payment.domain.model.transfer.*;
import com.tengju.payment.domain.model.transfer.citic.CiticHandleInfo;
import com.tengju.payment.domain.shard.Amount;
import com.tengju.payment.domain.shard.JsonUtil;
import com.tengju.payment.infrastructure.persistence.mybatis.dataobject.TradeRecordDO;

public class RepositoryTransferConvert {

    private RepositoryTransferConvert() {
    }

    public static Transfer doToDomain(TradeRecordDO tradeRecordDO) {
        if (tradeRecordDO == null) {
            return null;
        }
        HandleContext requestContext = null;
        HandleContext responseContext = null;

        //请求信息
        if (ChannelType.CITIC_BANK.getVal() == tradeRecordDO.getChannelType()) {
            requestContext = JsonUtil.toObject(tradeRecordDO.getRequestInfo(), CiticHandleInfo.class);
            responseContext = JsonUtil.toObject(tradeRecordDO.getResponseInfo(), CiticHandleInfo.class);
        }

        TransferExtra transferExtra = JsonUtil.toObject(tradeRecordDO.getExtra(), TransferExtra.class);
        return new Transfer(new TransferId(tradeRecordDO.getId()),
                BusinessType.fromVal(tradeRecordDO.getBusinessType()),
                tradeRecordDO.getChannelId() == 0 ? null : new ChannelId(tradeRecordDO.getChannelId()),
                ChannelType.fromVal(tradeRecordDO.getChannelType()),
                TradeStatus.fromVal(tradeRecordDO.getStatus()),
                new BusinessNo(tradeRecordDO.getBusinessNo()),
                tradeRecordDO.getTradeNo(),
                tradeRecordDO.getOutTradeNo(),
                new Amount(tradeRecordDO.getTradeAmount()),
                tradeRecordDO.getRequestTime(),
                tradeRecordDO.getCompleteTime(),
                requestContext,
                responseContext,
                transferExtra
        );
    }

    public static TradeRecordDO domainToDo(TransferForm transfer) {
        TradeRecordDO tradeRecordDO = new TradeRecordDO();
        tradeRecordDO.setBusinessType(transfer.getBusinessType().getVal());
        tradeRecordDO.setStatus(transfer.getTradeStatus().getVal());
        tradeRecordDO.setBusinessNo(transfer.getBusinessNo().getValue());
        tradeRecordDO.setTradeAmount((int) transfer.getTradeAmount().getValue());
        tradeRecordDO.setExtra(JsonUtil.toJSONString(transfer.getExtra()));
        return tradeRecordDO;
    }

    public static TradeRecordDO domainToDo(Transfer transfer) {
        TradeRecordDO updateTradeRecordDO = new TradeRecordDO();
        updateTradeRecordDO.setId(transfer.getTransferId().getValue());
        updateTradeRecordDO.setChannelId(transfer.getChannelId().getValue());
        updateTradeRecordDO.setChannelType(transfer.getChannelType().getVal());
        updateTradeRecordDO.setStatus(transfer.getTradeStatus().getVal());
        updateTradeRecordDO.setTradeNo(transfer.getTradeNo());
        updateTradeRecordDO.setOutTradeNo(transfer.getOutTradeNo());
        updateTradeRecordDO.setRequestTime(transfer.getRequestTime());
        updateTradeRecordDO.setCompleteTime(transfer.getCompleteTime());
        updateTradeRecordDO.setExtra(JsonUtil.toJSONString(transfer.getExtra()));
        return updateTradeRecordDO;
    }
}
